//
//  BDOrderDetailViewController.m
//  BeidouMall
//
//  Created by qie on 16/4/18.
//  Copyright © 2016年 qie. All rights reserved.
//

#import "BDOrderDetailViewController.h"
#import "BDOrderFooter.h"
#import "OrderTableViewCell.h"
#import "BDOrderListTableViewCell.h"
#import "BDRefundViewController.h"
#import "BDEvaluationViewController.h"

@interface BDOrderDetailViewController () <OrderTableViewCellDelegate>
{
    NSDictionary *statusDic;
    NSDictionary *expressTypeDic;
    NSDictionary *_orderDic;
    NSString *_bdreciveCode;
    
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableViewBottomSpace;
@property (weak, nonatomic) IBOutlet UIView *bottomView;

@end

@implementation BDOrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = NSLocalizedString(@"Order Detail", nil);
    
    statusDic = @{@"TO_PAY":NSLocalizedString(@"Wait to pay", nil),
                  @"PAYED":NSLocalizedString(@"Payed orders", nil),
                  @"RECEIVING":NSLocalizedString(@"Picking item", nil),
                  @"SENDING":NSLocalizedString(@"Sending item", nil),
                  @"COMFIRM_RECEIVE":NSLocalizedString(@"Comfirm receive", nil),
                  @"APPLY_REFUND":NSLocalizedString(@"Apply for a refund", nil),
                  @"REFUNDING":NSLocalizedString(@"Refunding", nil),
                  @"REFUNDED":NSLocalizedString(@"Refunded", nil),
                  @"CANCEL":NSLocalizedString(@"Cancelled", nil),
                  @"CLOSED":NSLocalizedString(@"Closed", nil),
                  @"COMFIRM_PAYING":NSLocalizedString(@"Confirming", nil)};
    
    expressTypeDic = @{@"BD":NSLocalizedString(@"Beidou Express", nil),
                       @"ONESELF":NSLocalizedString(@"Self-pick up", nil),
                       @"REMOTE":NSLocalizedString(@"Third party Express", nil)};
    
    NSString *orderStatus = _order[@"status"];
    if (![orderStatus isEqualToString:@"COMFIRM_RECEIVE"]) {// 已收货
        _bottomView.hidden = YES;
        _tableViewBottomSpace.constant = 0;
    }
    
    [self setTableViewHeader];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [_tableView.mj_header beginRefreshing];
    self.notRefreshHome = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark Private method

- (void)reloadData:(id) sender {
    if ([_order valueForKey:@"orderId"]) {
        NSString *url = [NSString stringWithFormat:@"%@%@?orderId=%@",
                         [BDNetworking shareInstance].baseUrl,
                         Order_Detail,
                         _order[@"orderId"]];
        [[BDNetworking shareInstance] postWithUrl:url sid:nil method:@"GET" parameters:nil completionHandler:^(NSObject *results, NSError *error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if ([_tableView.mj_header isRefreshing]) {
                    [_tableView.mj_header endRefreshing];
                }
            });
            if (error) {
                [MBProgressHUD showToast:self.view withText:NSLocalizedString(@"Order detail info failed", nil)];
            } else {
                NSLog(@"results = %@",results);
                _orderDic = (NSDictionary *)results;
                NSString *expressType = _orderDic[@"expressType"];
                NSString *status = _orderDic[@"status"];
                if (![expressType isEqual:[NSNull null]] && ![status isEqual:[NSNull null]]) {// 当订单为背兜超人(自取没有取货码)，且已经发货之后才会有取货码
                    if ([expressType isEqualToString:@"BD"] &&
                        ([status isEqualToString:@"SENDING"] || [status isEqualToString:@"COMFIRM_RECEIVE"])) {
                        [self getReciveCode];
                    }
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    [_tableView reloadData];
                });
            }
        }];
    }
    
}

- (void)setTableViewHeader {
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self reloadData:nil];
    }];
    if ([[BDAccountManagerModel shareInstance].langage isEqualToString:@"zang"]) {
        header.stateLabel.font = [UIFont fontWithName:@"Bzdbt" size:9.0];
    }
    
    [header setTitle:NSLocalizedString(@"Pull down to refresh", nil) forState:MJRefreshStateIdle];
    [header setTitle:NSLocalizedString(@"Release to refresh", nil) forState:MJRefreshStatePulling];
    [header setTitle:NSLocalizedString(@"Loading ...", nil) forState:MJRefreshStateRefreshing];
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    
    _tableView.mj_header = header;
}

- (void)getReciveCode {
    NSString *url = [NSString stringWithFormat:@"%@%@?orderId=%@",
                     [BDNetworking shareInstance].baseUrl,
                     BDreciveCode,
                     _order[@"orderId"]];
    [[BDNetworking shareInstance] postWithUrl:url sid:nil method:@"GET" parameters:nil completionHandler:^(NSObject *results, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if ([_tableView.mj_header isRefreshing]) {
                [_tableView.mj_header endRefreshing];
            }
        });
        if (error) {
            NSLog(@"获取背兜物流取货码失败");
        } else {
            NSLog(@"results = %@",results);
            _bdreciveCode = (NSString *)results;
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [_tableView reloadData];
            });
        }
    }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 1) {
        NSArray *goodList = _orderDic[@"orderCommodities"];
        return goodList.count;
    } else {
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        OrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"orderAddrCell"];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"BDOrderDetailAddressCell" owner:nil options:nil] lastObject];
        }
        UILabel *nameLabel = (UILabel *)[cell viewWithTag:101];
        UILabel *phoneLabel = (UILabel *)[cell viewWithTag:102];
        UITextView *addrTextView = (UITextView *)[cell viewWithTag:103];
        if (![_orderDic[@"userAddress"] isEqual:[NSNull null]]) {
            
            NSString *name = _orderDic[@"userAddress"][@"receiver"];
            if (![name isEqual:[NSNull null]]) {
                nameLabel.text = name;
            }
            // 电话
            NSString *phone = _orderDic[@"userAddress"][@"telephone"];
            if (phone.length >= 8) {
                phone = [phone stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
                if (![phone isEqual:[NSNull null]]) {
                    phoneLabel.text = phone;
                }
            }
            // 地址
            NSString *region = _orderDic[@"userAddress"][@"region"];
            NSString *detaiAddr = _orderDic[@"userAddress"][@"detailAddress"];
            if (![region isEqual:[NSNull null]] &&
                ![detaiAddr isEqual:[NSNull null]] &&
                region != nil &&
                detaiAddr != nil) {
                addrTextView.text = [NSString stringWithFormat:@"%@ %@",region,detaiAddr];
            } else {
                addrTextView.text = @"";
            }
        } else {
            nameLabel.text = @"姓名未获取";
            phoneLabel.text = @"联系方式未获取";
            addrTextView.text = @"地址未获取";
        }
        
        return cell;
    } else if (indexPath.section == 1) {
        OrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"orderCell"];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"OrderCell" owner:nil options:nil] lastObject];
        }
        
        cell.delegate = self;
        cell.backgroundColor = RGBColor(255, 255, 255);
        [[cell viewWithTag:301] setHidden:NO];
        
        NSArray *goodList = _orderDic[@"orderCommodities"];
        // 商品图片
        NSString *storeImageUrl = goodList[indexPath.row][@"commodity"][@"icon"];
        UIImageView *storeImageView = (UIImageView *)[cell viewWithTag:101];
        if (![storeImageUrl isEqual:[NSNull null]]) {
            NSInteger width = storeImageView.width * 2;
            NSInteger height = storeImageView.height * 2;
            storeImageUrl = [NSString stringWithFormat:@"%@?dp_cmd=/imageView2/mode/2/w/%ld/h/%ld/",storeImageUrl,(long)width,(long)height];
            [storeImageView sd_setImageWithURL:[NSURL URLWithString:storeImageUrl] placeholderImage:[UIImage imageNamed:@"image_empty"]];
        }
        // 商品名称
        UITextView *goodDescTextView = (UITextView *)[cell viewWithTag:102];
        NSString *goodDesc = goodList[indexPath.row][@"commodity"][@"name"];
        if (![goodDesc isEqual:[NSNull null]]) {
            goodDescTextView.text = goodDesc;
        }
        // 提示取货店铺
        UILabel *shopLabel = (UILabel *)[cell viewWithTag:106];
        shopLabel.hidden = YES;
        NSDictionary *goodDic = goodList[indexPath.row][@"commodity"];
        NSString *expressType = _orderDic[@"expressType"];
        for (NSString *key in goodDic.allKeys) {
            if ([key isEqualToString:@"oneselfAddress"] && [expressType isEqualToString:@"ONESELF"]) {
                NSString *value = goodDic[key];
                if (value.length > 0) {
                    for (NSLayoutConstraint *constraint in cell.contentView.constraints) {
                        if ([constraint.identifier isEqualToString:@"textViewBottom"]) {
                            constraint.constant = 64.0;
                        }
                        if ([constraint.identifier isEqualToString:@"buttonBottomSpace"]) {
                            constraint.constant = 24.0;
                        }
                    }
                    shopLabel.hidden = NO;
                    shopLabel.text = [NSString stringWithFormat:@"自取地址：%@",value];
                }
            }
        }
        
        
        // 商品现价
        UILabel *priceNowLabel = (UILabel *)[cell viewWithTag:103];
        NSString *goodNowPrice = goodList[indexPath.row][@"price"];
        if (![goodNowPrice isEqual:[NSNull null]]) {
            priceNowLabel.text = [NSString stringWithFormat:@"%@¥%.2f",NSLocalizedString(@"Price", nil),[goodNowPrice floatValue]];
        }
        // 商品个数
        UILabel *numLabel = (UILabel *)[cell viewWithTag:104];
        NSString *goodNum = goodList[indexPath.row][@"num"];
        if (![goodNum isEqual:[NSNull null]]) {
            numLabel.text = [NSString stringWithFormat:@"%@x%ld",NSLocalizedString(@"OrderAmount", nil),(long)[goodNum integerValue]];
        }
        // 申请退货
        UIButton *refundBtn = [(UIButton *)cell viewWithTag:105];
        NSString *orderStatus = _orderDic[@"status"];
        NSString *goodStatus = goodList[indexPath.row][@"status"];
        if ([orderStatus isEqualToString:@"TO_PAY"] ||
            [orderStatus isEqualToString:@"CLOSED"] ||
            [orderStatus isEqualToString:@"CANCEL"]) {
            [refundBtn setHidden:YES];
        } else {
            [refundBtn setHidden:NO];
        }
        if ([[BDAccountManagerModel shareInstance].langage isEqualToString:@"en"]) {
            cell.buttonWidthConstrains.constant = 120.0;
        } else if ([[BDAccountManagerModel shareInstance].langage isEqualToString:@"zh"]) {
            cell.buttonWidthConstrains.constant = 66.0;
        }
        
        if ([goodStatus isEqualToString:@"CLOSED"] || [goodStatus isEqualToString:@"CLOSE"]) {// 关闭退款 此时不显示退款按钮
            [refundBtn setHidden:YES];
        }
        
        if ([goodStatus isEqualToString:@"NORMAL"] ||
            [goodStatus isEqualToString:@"REFUSE_REFUND"]) {// 正常状态 拒绝退款
            [refundBtn setTitle:NSLocalizedString(@"Apply for a refund", nil) forState:UIControlStateNormal];
        } else {
            NSString *statusStr;
            if ([goodStatus isEqualToString:@"APPLY_REFUND"]) {
                statusStr = NSLocalizedString(@"Have applied a refund", nil);
            }
            if ([goodStatus isEqualToString:@"REFUNDING"]) {
                statusStr = NSLocalizedString(@"Refunding", nil);
            }
            if ([goodStatus isEqualToString:@"REFUND"]) {
                statusStr = NSLocalizedString(@"Refunded", nil);
            }
            [refundBtn setTitle:statusStr forState:UIControlStateNormal];
            
        }
        return cell;
    } else {
        BDOrderListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"orderListCell"];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"BDOrderDetailListCell" owner:nil options:nil] lastObject];
        }
        
        UILabel *label1 = (UILabel *)[cell viewWithTag:101];
        UITextView *textView2 = (UITextView *)[cell viewWithTag:102];
        UILabel *label3 = (UILabel *)[cell viewWithTag:103];
        UILabel *label4 = (UILabel *)[cell viewWithTag:104];
        UILabel *label5 = (UILabel *)[cell viewWithTag:105];
        UILabel *label6 = (UILabel *)[cell viewWithTag:106];
        
        NSString *gmtPayment = _orderDic[@"orderTrade"][@"gmtPayment"];
        NSString *tradeNo = _orderDic[@"orderTrade"][@"tradeNo"];
        NSString *buyerEmail = _orderDic[@"orderTrade"][@"buyerEmail"];
        NSString *payType = _orderDic[@"payType"];
        if (![payType isEqual:[NSNull null]]) {
            if ([payType isEqualToString:@"WECHAT"]) {
                buyerEmail = @"";
            }
        }
        cell.firstLabelTopSpace.constant = 8.0;
        cell.textViewTopSpace.constant = 28.0;
        label6.hidden = YES;
        if (![gmtPayment isEqual:[NSNull null]] &&
            ![tradeNo isEqual:[NSNull null]] &&
            ![buyerEmail isEqual:[NSNull null]] &&
            gmtPayment != nil &&
            tradeNo != nil &&
            buyerEmail != nil) {
            label3.hidden = NO;
            label4.hidden = NO;
            label5.hidden = NO;
            // 背兜物流取货码
            if (_bdreciveCode.length > 0) {
                cell.firstLabelTopSpace.constant = 28.0;
                cell.textViewTopSpace.constant = 48.0;
                label6.hidden = NO;
                label6.text = [NSString stringWithFormat:@"%@%@",NSLocalizedString(@"Pick up code", nil),_bdreciveCode];
            }
            // 订单编号
            NSString *orderID = _orderDic[@"orderId"];
            if (![orderID isEqual:[NSNull null]]) {
                label1.text = [NSString stringWithFormat:@"%@%@",NSLocalizedString(@"Order number", nil),orderID];
            }
            // 支付宝交易号
            NSString *payType = _orderDic[@"payType"];
            if (![payType isEqual:[NSNull null]]) {
                if ([payType isEqualToString:@"ALIPAY"]) {
                    textView2.text = [NSString stringWithFormat:@"%@%@",NSLocalizedString(@"Alipay transaction number", nil),tradeNo];
                }
                if ([payType isEqualToString:@"WECHAT"]) {// 微信不能获取买家账号，所以结构要调整
                    textView2.text = [NSString stringWithFormat:@"%@%@",NSLocalizedString(@"Wechat transaction number", nil),tradeNo];
                    cell.textViewBottomSapce.constant = 62.0;
                }
            }
            // 买家账号
            if (buyerEmail.length > 0) {
                label3.text = [NSString stringWithFormat:@"%@%@",NSLocalizedString(@"Pay account", nil),buyerEmail];
            } else {
                label3.text = @"";
                label3.hidden = YES;
            }
            
            // 创建时间
            NSString *gmtCreate = _orderDic[@"gmtCreate"];
            if (![gmtCreate isEqual:[NSNull null]]) {
                label4.text = [NSString stringWithFormat:@"%@%@",NSLocalizedString(@"Creat time", nil),[NSString converToDateString:([gmtCreate doubleValue] / 1000) formatter:@"yyyy-MM-dd HH:mm:ss"]];
            }
            // 付款时间
            label5.text = [NSString stringWithFormat:@"%@%@",NSLocalizedString(@"Pay time", nil),[NSString converToDateString:([gmtPayment doubleValue] / 1000)formatter:@"yyyy-MM-dd HH:mm:ss"]];
            
        } else {
            // 订单编号
            NSString *orderID = _orderDic[@"orderId"];
            if (![orderID isEqual:[NSNull null]] &&
                orderID != nil) {
                label1.text = [NSString stringWithFormat:@"%@%@",NSLocalizedString(@"Order number", nil),orderID];
            }
            // 创建时间
            NSString *gmtCreate = _orderDic[@"gmtCreate"];
            if (![gmtCreate isEqual:[NSNull null]] &&
                gmtCreate != nil) {
                textView2.text = [NSString stringWithFormat:@"%@%@",NSLocalizedString(@"Creat time", nil),[NSString converToDateString:([gmtCreate doubleValue] / 1000) formatter:@"yyyy-MM-dd HH:mm:ss"]];
                cell.textViewBottomSapce.constant = 0.0;
            }
            label3.hidden = YES;
            label4.hidden = YES;
            label5.hidden = YES;
        }
        
        return cell;
    }
}

#pragma mark - Tableview delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        CGFloat textViewHeight = 35.0;
        if (![_orderDic[@"userAddress"] isEqual:[NSNull null]]) {
            NSString *region = _orderDic[@"userAddress"][@"region"];
            NSString *detaiAddr = _orderDic[@"userAddress"][@"detailAddress"];
            if (![region isEqual:[NSNull null]] &&
                ![detaiAddr isEqual:[NSNull null]]) {
                NSString *addr = [NSString stringWithFormat:@"%@ %@",region,detaiAddr];
                textViewHeight = [addr boundingHeightWithSize:CGSizeMake(kScreen_Width - 39.0, CGFLOAT_MAX) font:[UIFont systemFontOfSize:14.0] lineSpace:8.0];
                textViewHeight = textViewHeight < 35.0 ? 35.0 : textViewHeight;
            }
            return textViewHeight + 47.0;
        }
        return 82.0;
    } else if (indexPath.section == 1) {
        CGFloat cellHeight = 100.0;
        NSArray *goodList = _orderDic[@"orderCommodities"];
        NSString *goodDesc = goodList[indexPath.row][@"commodity"][@"name"];
        if (![goodDesc isEqual:[NSNull null]]) {
            cellHeight = [goodDesc boundingHeightWithSize:CGSizeMake(kScreen_Width - 104.0, CGFLOAT_MAX) font:[UIFont systemFontOfSize:12.0] lineSpace:8.0] + 55;
            cellHeight = cellHeight < 100.0 ? 100.0 : cellHeight;
        }
        NSDictionary *goodDic = goodList[indexPath.row][@"commodity"];
        NSString *expressType = _orderDic[@"expressType"];
        for (NSString *key in goodDic.allKeys) {
            if ([key isEqualToString:@"oneselfAddress"] && [expressType isEqualToString:@"ONESELF"]) {
                NSString *value = goodDic[key];
                if (value.length > 0) {
                    return cellHeight + 20.0;
                }
            }
        }
        return cellHeight;
    } else {
        NSString *gmtPayment = _orderDic[@"orderTrade"][@"gmtPayment"];
        NSString *tradeNo = _orderDic[@"orderTrade"][@"tradeNo"];
        NSString *buyerEmail = _orderDic[@"orderTrade"][@"buyerEmail"];
        NSString *payType = _orderDic[@"payType"];
        if (![payType isEqual:[NSNull null]]) {
            if ([payType isEqualToString:@"WECHAT"]) {
                buyerEmail = @"";
            }
        }
        if (![gmtPayment isEqual:[NSNull null]] &&
            ![tradeNo isEqual:[NSNull null]] &&
            ![buyerEmail isEqual:[NSNull null]]) {
            CGFloat textViewHeight = 0.0;
            NSString *title = [payType isEqualToString:@"WECHAT"] ? NSLocalizedString(@"Wechat transaction number", nil) : NSLocalizedString(@"Alipay transaction number", nil);
            NSString *str = [NSString stringWithFormat:@"%@%@",title,tradeNo];
            textViewHeight = [str boundingHeightWithSize:CGSizeMake(kScreen_Width - 3.0, CGFLOAT_MAX) font:[UIFont systemFontOfSize:14.0] lineSpace:8.0] + 20;
            if (_bdreciveCode.length > 0) {
                textViewHeight += 20;
            }
            return 96.0 + textViewHeight;
        }
        return 60.0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.0;
    } else if (section == 1) {
        if (_orderDic) {
            NSString *storeName = _orderDic[@"store"][@"name"];
            CGFloat labelHeight = 17.0;
            if (![storeName isEqual:[NSNull null]]) {
                labelHeight = [storeName boundingHeightWithSize:CGSizeMake(kScreen_Width - 91.0, CGFLOAT_MAX) font:[UIFont systemFontOfSize:14.0] lineSpace:8.0];
            }
            if (labelHeight <= 17.0) {
                labelHeight = 17.0;
            }
            return labelHeight + 18.0;
        }
        return 0.1;
    } else {
        return 0.0;
    }
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        UIView *header = [[[NSBundle mainBundle] loadNibNamed:@"orderHeader" owner:nil options:nil] lastObject];
        header.backgroundColor = RGBColor(215, 19, 33);
        // 商店名称
        NSString *storeName = _orderDic[@"store"][@"name"];
        if (![storeName isEqual:[NSNull null]]) {
            ((UILabel *)[header viewWithTag:101]).text = storeName;
            ((UILabel *)[header viewWithTag:101]).textColor = RGBColor(255, 255, 255);
        }
        // 订单当前状态
        UILabel *statusLabel = (UILabel *)[header viewWithTag:102];
        statusLabel.hidden = YES;
//        NSString *orderStatus = _orderDic[@"status"];
//        
//        if ([statusDic valueForKey:orderStatus] &&
//            ![orderStatus isEqual:[NSNull null]]) {
//            statusLabel.text = statusDic[orderStatus];
//        }
        UIImageView *imageView = (UIImageView *)[header viewWithTag:103];
        imageView.image = [UIImage imageNamed:@"beidou_shop2"];
        for (NSLayoutConstraint *constraint in imageView.constraints) {
            if (constraint.firstAttribute == NSLayoutAttributeHeight) {
                constraint.constant = 20.0;
            }
        }
        
        return header;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 0) {
        return 0.0;
    } else if (section == 1) {
        return 73.0;
    } else {
        return 0.0;
    }
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 1) {
        BDOrderFooter *footer = [[[NSBundle mainBundle] loadNibNamed:@"orderDetailFooter" owner:nil options:nil] lastObject];
        // 物流名称
        NSDictionary *keyDic = @{@"OLD_CITY":@"老城区",@"NEW_CITY":@"新城区",@"SOUTH":@"南郊"};
        NSString *expressType = _orderDic[@"expressType"];
        NSString *bdAddress = _orderDic[@"bdAddress"];
        if (![expressType isEqual:[NSNull null]] &&
            [expressTypeDic valueForKey:expressType]) {
            if ([expressType isEqualToString:@"BD"]) {
                if (![bdAddress isEqual:[NSNull null]] && bdAddress.length > 0) {
                    ((UILabel *)[footer viewWithTag:103]).text = [NSString stringWithFormat:@"%@(%@%@%@)",NSLocalizedString(@"Express fee", nil),expressTypeDic[expressType],NSLocalizedString(@"to", nil),keyDic[bdAddress]];
                } else {
                    ((UILabel *)[footer viewWithTag:103]).text = [NSString stringWithFormat:@"%@(%@%@%@)",NSLocalizedString(@"Express fee", nil),expressTypeDic[expressType],NSLocalizedString(@"to", nil),@"老城区"];
                }
            } else {
                ((UILabel *)[footer viewWithTag:103]).text = [NSString stringWithFormat:@"%@(%@)",NSLocalizedString(@"Express fee", nil),expressTypeDic[expressType]];
            }
            
        }
        // 运费
        NSString *expressPrice = _orderDic[@"expressPrice"];
        if (![expressPrice isEqual:[NSNull null]]) {
            ((UILabel *)[footer viewWithTag:101]).text = [NSString stringWithFormat:@"¥%.2f",[expressPrice floatValue]];
        }
        // 订单费用
        NSString *commodityPrice = _orderDic[@"commodityPrice"];
        if (![commodityPrice isEqual:[NSNull null]] &&
            ![expressPrice isEqual:[NSNull null]]) {
            ((UILabel *)[footer viewWithTag:102]).text = [NSString stringWithFormat:@"¥%.2f",[commodityPrice floatValue] + [expressPrice floatValue]];
        }
        // 实际付款
        ((UILabel *)[footer viewWithTag:104]).text = NSLocalizedString(@"Amount to pay(Include Express fee)", nil);
        
        return footer;
    }
    return nil;
}

#pragma mark OrderTableViewCellDelegate

- (void)applyRefund:(UIView *)sender {// 退货
    UIView *superView = sender.superview;
    while (![superView isKindOfClass:[UITableViewCell class]]) {
        superView = superView.superview;
    }
    UITableViewCell *cell = (UITableViewCell *)superView;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    NSArray *goodList = _orderDic[@"orderCommodities"];
    UIStoryboard *stroyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    BDRefundViewController *refund_vc = [stroyboard instantiateViewControllerWithIdentifier:@"RefunStoryboardID"];
    refund_vc.goodList = goodList;
    refund_vc.index = indexPath.row;
    refund_vc.block = ^(NSString *str){
        if ([str isEqualToString:@"success"]) {
            self.notRefreshHome = NO;
            dispatch_async(dispatch_get_main_queue(), ^{
                [_tableView.mj_header beginRefreshing];
            });
        }
    };
    [self.navigationController pushViewController:refund_vc animated:YES];
}

#pragma mark - IBActions

- (IBAction)evaluationButtonPressed:(UIButton *)sender {// 评价
    NSString *orderId = _orderDic[@"orderId"];
    NSArray *commodities = _orderDic[@"orderCommodities"];
    
    UIStoryboard *stroyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    BDEvaluationViewController *eva_vc = [stroyboard instantiateViewControllerWithIdentifier:@"EvaluationStoryboardID"];
    eva_vc.orderId = orderId;
    eva_vc.commodities = commodities;
    [self.navigationController pushViewController:eva_vc animated:YES];
}


/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
